using System;
using System.Collections.Generic;
using System.Text;

namespace Diffluxum.CCScoring
{
    public class ProportionalGradingRule : IGradingRule
    {
        private int _pointsPerGreenToGreenBuilds;
        private int _pointsPerGreenToRedBuilds;
        private int _pointsPerRedToGreenBuilds;
        private int _pointsPerRedToRedBuilds;
        private int _pointsPerCheckinsToRedBuild;
        private int _pointsPerHourBreakingBuild;
        private int _pointsPerHourCheckedInToBreakingBuild;

        public ProportionalGradingRule(
             int pointsPerGreenToGreenBuilds,
             int pointsPerGreenToRedBuilds,
             int pointsPerRedToGreenBuilds,
             int pointsPerRedToRedBuilds,
             int pointsPerCheckinsToRedBuild,
             int pointsPerHourBreakingBuild,
             int pointsPerHourCheckedInToBreakingBuild
            )
        {
            _pointsPerGreenToGreenBuilds = pointsPerGreenToGreenBuilds;
            _pointsPerGreenToRedBuilds = pointsPerGreenToRedBuilds;
            _pointsPerRedToGreenBuilds = pointsPerRedToGreenBuilds;
            _pointsPerRedToRedBuilds = pointsPerRedToRedBuilds;
            _pointsPerCheckinsToRedBuild = pointsPerCheckinsToRedBuild;
            _pointsPerHourBreakingBuild = pointsPerHourBreakingBuild;
            _pointsPerHourCheckedInToBreakingBuild = pointsPerHourCheckedInToBreakingBuild;
        }

        #region IGradingRule Members

        public void GradeUser(IUserScorer _user)
        {
            int greenToGreenBuilds = _user.GreenToGreenBuilds;
            int greenToRedBuilds = _user.GreenToRedBuilds;
            int redToGreenBuilds = _user.RedToGreenBuilds;
            int redToRedBuilds = _user.RedToRedBuilds;
            int checkinsToRedBuild = _user.CheckinsToRedBuild;

            int secondsBreaking = (int)_user.TotalCausedBreakingTime.TotalSeconds;
            int secondsCheckedInToBreaking = (int)_user.TotalSubmittedToBreakingTime.TotalSeconds;


            _user.AddGrade(_pointsPerCheckinsToRedBuild * checkinsToRedBuild,
                    String.Format(
                     "Check ins to red builds caused by other user ({0}) * points per check in ({1}) = {2}",
                    checkinsToRedBuild, _pointsPerCheckinsToRedBuild, checkinsToRedBuild * _pointsPerCheckinsToRedBuild)
                    );

            _user.AddGrade(greenToGreenBuilds * _pointsPerGreenToGreenBuilds,
                String.Format(
                 "Check ins causing green build to stay green ({0}) * points per check in ({1}) = {2}",
                greenToGreenBuilds, _pointsPerGreenToGreenBuilds, greenToGreenBuilds * _pointsPerGreenToGreenBuilds)
                );

            _user.AddGrade(greenToRedBuilds * _pointsPerGreenToRedBuilds,
                String.Format(
                 "Check ins causing green build to turn red ({0}) * points per check in ({1}) = {2}",
                greenToRedBuilds, _pointsPerGreenToRedBuilds, greenToRedBuilds * _pointsPerGreenToRedBuilds)
                );

            _user.AddGrade(redToRedBuilds * _pointsPerRedToRedBuilds,
                String.Format(
                 "Check ins causing red build to remain red ({0}) * points per check in ({1}) = {2}",
                redToRedBuilds, _pointsPerRedToRedBuilds, redToRedBuilds * _pointsPerRedToRedBuilds)
                );

            _user.AddGrade(redToGreenBuilds * _pointsPerRedToGreenBuilds,
                String.Format(
                 "Check ins causing red build to turn green ({0}) * points per check in ({1}) = {2}",
                redToGreenBuilds, _pointsPerRedToGreenBuilds, redToGreenBuilds * _pointsPerRedToGreenBuilds)
                );

            _user.AddGrade(secondsBreaking * _pointsPerHourBreakingBuild / 3600,
                    String.Format(
                     "Total hours red time ({0}) * points per hour ({1}) = {2}",
                    ((float)secondsBreaking) / 3600, _pointsPerHourBreakingBuild, secondsBreaking * _pointsPerHourBreakingBuild / 3600)
                    );

            _user.AddGrade(secondsCheckedInToBreaking * _pointsPerHourCheckedInToBreakingBuild / 3600,
                String.Format(
                 "Total hours red time after check in to already red build ({0}) * points per hour ({1}) = {2}",
                ((float)secondsCheckedInToBreaking) / 3600, _pointsPerHourCheckedInToBreakingBuild, secondsCheckedInToBreaking * _pointsPerHourCheckedInToBreakingBuild / 3600)
                );
        }

        #endregion
    }
}
